import { useState, useEffect } from 'react';
import { message } from 'antd';
import { fetchVersionDetaillist, fetchPostDetaillist } from '@/services/setting/salaryReference';

// 版本记录详情
const useEditionDetailList = params => {
    const {
        pagination = {},
        payloads = {},
        setPagination = () => {},
        search = '',
        urlType = 'salary',
        sortCustom = '',
        filterDataName = [],
        setExpkeys = () => {}
    } = params;
    const { version = '', ...otherPayloads } = payloads;
    const { current = 1, pageSize = 10 } = pagination;
    const [loading, setLoading] = useState(false);
    const [resData, setResData] = useState(null);

    const fetchData = async () => {
        setLoading(true);
        const salaryPayload = {
            salaryStandardVersion: version,
            orderByField: sortCustom,
            salaryStandardNames: filterDataName,
            pageSize,
            pageNo: current,
            ...otherPayloads
        };
        const postPayload = {
            postitionSalaryVersion: version,
            pageSize,
            pageNo: current,
            ...otherPayloads
        };
        const { data, code, msg } =
            urlType === 'salary' ? await fetchVersionDetaillist(salaryPayload) : await fetchPostDetaillist(postPayload);
        if (code === 10000) {
            setResData(data.rows);
            setExpkeys(data.rows && data.rows.map(item => item.id));
            setPagination({ ...pagination, total: data.total });
        } else if (msg && msg.length > 0) {
            message.error(msg);
        }
        setLoading(false);
    };
    useEffect(() => {
        if (version) {
            fetchData();
        }
    }, [version, search, current, pageSize, sortCustom, filterDataName]);
    return [loading, resData];
};

export default useEditionDetailList;
